define(["jquery", "i18nCommon", "box"], function ($, i18n, box) {

    var Http;

    Http = function (url, settings, syncSuccessFn, syncErrorFn, context) {
        this.settings = $.extend({}, Http.defaultSettings, settings);
        if (url.indexOf(cywork.global.rootPath) < 0) {
            url = cywork.global.rootPath + url;
        }
        // if (url.indexOf(i18n.textSystemSuffix) < 0) {
        //     url += i18n.textSystemSuffix;
        // }
        this.settings.url = url;
        if (!this.settings.async) {
            if (context) {
                if (syncSuccessFn) {
                    syncSuccessFn = $.proxy(syncSuccessFn, context);
                }
                if (syncErrorFn) {
                    syncErrorFn = $.proxy(syncErrorFn, context);
                }
            }
            this._successFn = syncSuccessFn;
            this._errorFn = syncErrorFn;
        }
        this.settings.success = $.proxy(Http._success, this);
        this.settings.error = $.proxy(Http._errorHandler, this);
    };

    Http.const = {
        json: "application/json; charset=utf-8",
        urlencoded: "application/x-www-form-urlencoded; charset=utf-8"
    };

    Http.defaultSettings = {
        async: true,       //同步还是异步，默认为异步
        cache: false,
        timeout: 1200000,
        type: "GET",       //请求方式 ("POST" 或 "GET"), 注意：其它 HTTP 请求方法，如 PUT 和  DELETE 也可以使用，但仅部分浏览器支持。
        global: true,
        contentType: Http.const.urlencoded,
        jsonp: null,
        complete: function (data) {
        },
        dataFilter: function (data, type) {
            return data;
        },
        dataType: "json",
        username: null,
        password: null,
        processData: true,
        shutDownShowMsgFlag: false,
        shutDownRefreshFlag: false
    };

    Http._success = function (data) {
        if (data.code === 0) {
            this._successFn(data.data, data)
        } else {
            Http._error(data, this)
        }
    };

    Http._errorHandler = function (XMLHttpRequest, textStatus, errorThrown) {
        //如果登陆被踢，或者session失效和没有登陆
        if (XMLHttpRequest && XMLHttpRequest.status === 401) {
            cywork.global.httpSessionValidFlag = false;
            box.confirm({
                title: i18n.textCommonBoxAlertTitle,
                message: XMLHttpRequest.responseJSON[0].message,
                callback: function (result) {
                    if (result) {
                        window.location = cywork.global.rootPath;
                    }
                }
            });

        } else if (XMLHttpRequest && XMLHttpRequest.status === 0) {
            if (this.settings.shutDownShowMsgFlag) {
                var refreshFunc = $.proxy(function () {
                    if (this.settings.shutDownRefreshFlag) {
                        window.location = cywork.global.rootPath;
                    }
                }, this);

                box.confirm({
                    title: i18n.textCommonBoxPrompt,
                    message: i18n.textCommonShutDown,
                    callback: function (result) {
                        if (result) {
                            refreshFunc();
                        }
                    }
                });
            }
        } else {
            Http._error({
                "data": {
                    "XMLHttpRequest": XMLHttpRequest,
                    "textStatus": textStatus,
                    "errorThrown": errorThrown
                },
                "success": false,
                "message": i18n.textCommonError
            }, this);
        }
    };

    Http._error = function (data, context) {
        if (context._errorFn) {
            if (data) {
                context._errorFn(data.data, data);
            } else {
                context._errorFn();
            }
        } else {
            if (data && data.message) {
                box.alert(data.message)
            } else {
                box.alert(i18n.textCommonError);
            }
        }
    };

    Http.prototype.ajax = function (data) {
        this.settings.data = data;
        $.ajax(this.settings);
        return this;
    };

    Http.prototype.then = function (successFn, context) {
        if (this.settings.async) {
            if (context) {
                successFn = $.proxy(successFn, context);
            }
            this._successFn = successFn;
        } else {
            box.alert(" sync can not use then() ");
        }
        return this;
    };

    Http.prototype.error = function (errorFn, context) {
        if (this.settings.async) {
            if (context) {
                errorFn = $.proxy(errorFn, context);
            }
            this._errorFn = errorFn;
        } else {
            box.alert(" sync can not use error() ");
        }
        return this;
    };

    return {
        getInstance: function (url, settings, syncSuccessFn, syncErrorFn, context) {
            return new Http(url, settings, syncSuccessFn, syncErrorFn, context);
        }
    }
});